package com.ccloud.auth.core.config;

import com.ccloud.auth.core.handler.CAuthenticationFailureHandler;
import com.ccloud.auth.core.handler.CAuthenticationSuccessHandler;
import com.ccloud.auth.core.handler.CLogoutHandler;
import com.ccloud.auth.core.handler.CLogoutSuccessHandler;
import com.ccloud.auth.modules.service.impl.JwtTokenUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.annotation.Resource;

/**
 * @author scylla
 * spring security的安全配置
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private CAuthenticationSuccessHandler authenticationSuccessHandler;

    @Resource
    private CAuthenticationFailureHandler authenticationFailureHandler;

    @Resource
    private JwtTokenUserDetailsService userDetailsService;

    @Resource
    private LogoutHandler logoutHandler;

    @Resource
    private LogoutSuccessHandler logoutSuccessHandler;
    /**
     * 加密算法
     */
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 允许表单登录
        http.authorizeRequests()
                //注销的接口需要放行
                .antMatchers("/oauth/captcha/**", "/oauth/login","/oauth/login",
                        "/oauth/loginfo", "/oauth/visitInfo", "/oauth/sms", "/oauth/phoneLogin").permitAll()
                .anyRequest().authenticated()
                .and()
                .logout().logoutUrl("/oauth/logout").addLogoutHandler(logoutHandler).logoutSuccessHandler(logoutSuccessHandler).permitAll()
                .and()
                .formLogin()
                .loginProcessingUrl("/oauth/login").successHandler(authenticationSuccessHandler).failureHandler(authenticationFailureHandler).permitAll()
                .and()
                .csrf()
                .disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //从数据库中查询用户信息
        auth.userDetailsService(userDetailsService);
    }



    /**
     * AuthenticationManager对象在OAuth2认证服务中要使用，提前放入IOC容器中
     * Oauth的密码模式需要
     */
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}